﻿using System;
using System.IO;
using Comdiv.QWeb.Files;
using Comdiv.QWeb.ViewEngine;
using NUnit.Framework;

namespace Comdiv.QWeb.Test.ViewEngine {
    [TestFixture]
    public class compile_simple_process_test
    {
        public class testdata
        {
            public int val;
        }
        [Action("csp.test")]
        public class testaction : IAction
        {
            [Bind]
            private int retval;
            public object Process(QWebContext context)
            {
                return new testdata { val = retval };
            }
        }

        private string code =
            @"
        using Comdiv.QWeb.Test.ViewEngine;
        public partial class VIEWCLS : BASECLS
        {
            [QViewBind]private int val;
            [ViewData]private compile_simple_process_test.testdata item;
            protected override void render()
            {
                outp(""<div>val:"");
                outp(val);
                outp(""</div><div>itemtype:"");
                outp(item.GetType().Name);
                outp(""</div>"");
            }
        }";

        
        [Test]
        public void test_view_compiled_from_cs()
        {
            var reg = new QWebServiceRegistry();
            reg.FileNameResolver.Write("/qviews/csp/test.cs", code);
            var h = new QWebHandler(reg);
            var sw = new StringWriter();
            var ctx = new QWebContext("http://localhost/csp/test.qview.qweb").Set("retval", 45);
            ctx.Output = sw;
            h.ProcessRequest(ctx);
            Console.WriteLine(sw.ToString());
            Assert.AreEqual("<div>val:45</div><div>itemtype:testdata</div>", sw.ToString());

            sw =new StringWriter();
            ctx.Output = sw;
            ctx.Set("retval", 56);
            h.ProcessRequest(ctx);
            Console.WriteLine(sw.ToString());
            Assert.AreEqual("<div>val:56</div><div>itemtype:testdata</div>", sw.ToString());
            Assert.AreEqual(1,((QViewCompiler)reg.QViewCompiler).CompileCount);
        }
    }
}